package com.huanwei.cc.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SearchView;
import android.widget.TextView;
import android.widget.Toast;

import com.huanwei.cc.R;
import com.huanwei.cc.bean.WDqd;
import com.huanwei.cc.utils.JSONUtils;
import com.huanwei.cc.utils.Validator;
import com.huanwei.http.API;
import com.huanwei.http.Okhttp;
import com.huanwei.login_regist.Curriculum_Activity;
import com.huanwei.login_regist.ZuoYeGuiJi_10_Activity;
import com.huanwei.me_question.adapter.QD_Adapter;
import com.whmnx.fishfamily.MyMapActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by ADM on 2017/12/2.
 * 嫌疑人追踪
 */

public class XianYiRen_activity extends FragmentActivity {


    @BindView(R.id.img_back)
    RelativeLayout imgBack;
    @BindView(R.id.button2)
    Button button2;
    @BindView(R.id.lv_list)
    ListView lvList;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.button3)
    Button button3;
    @BindView(R.id.button5)
    Button button5;
    @BindView(R.id.search)
    SearchView search;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_xianyiren);
        ButterKnife.bind(this);
        title.setText("嫌疑人追踪");
        button3.setVisibility(View.GONE);
        button5.setVisibility(View.GONE);
        initViewsAndEvents();
        getCurriculums();
        search.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                getUser(query);
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                return false;
            }
        });
    }

    private void getUser(String query) {
        Map<String, String> map = new HashMap<>();
        boolean idCard = Validator.isIDCard(query);

        if (idCard) {
            map.put("shenfenzheng", query);
            map.put("name", "");
        } else {
            map.put("shenfenzheng", "");
            map.put("name", query);
        }
        API.sousuo(map, new Okhttp.Objectcallback() {
            @Override
            public void onsuccess(String st) {
                bank_card_adapter.clear();
                bank_card_adapter.notifyDataSetChanged();
                WDqd teacher = JSONUtils.parseJSON(st, WDqd.class);
                if (!teacher.getTotal().equals("0")) {

                    ccList = teacher.getRows();

                    bank_card_adapter.addItem(ccList);

                } else {
                    Toast.makeText(XianYiRen_activity.this, "暂无数据", Toast.LENGTH_SHORT).show();

                }

            }

            @Override
            public void onFalia(int code, String errst) {
                Toast.makeText(XianYiRen_activity.this, "网络错误！", Toast.LENGTH_SHORT).show();
            }
        });

    }

    QD_Adapter bank_card_adapter;

    public void initViewsAndEvents() {
//        初始化返回按钮和考勤列表
        RelativeLayout img_back = (RelativeLayout) findViewById(R.id.img_back);//设置;
        img_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //点击返回按钮
                finish();
            }
        });
        ListView lvList = (ListView) findViewById(R.id.lv_list);//设置列表;
        lvList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                WDqd.RowsBean item = (WDqd.RowsBean) bank_card_adapter.getItem(i);
                startActivity(new Intent(XianYiRen_activity.this, MyMapActivity.class).putExtra("name", item.getUserNm()));

            }
        });

//        增加列表适配器
        bank_card_adapter = new QD_Adapter();
        lvList.setAdapter(bank_card_adapter);


    }

    List<WDqd.RowsBean> ccList = new ArrayList<>();


    public void getCurriculums() {
        ccList.clear();
//        根据用户ID查询考勤记录
        API.getSignbystudentid(new Okhttp.Objectcallback() {
            @Override
            public void onsuccess(String st) {
                Log.e("111", "xx" + st);


                WDqd teacher = JSONUtils.parseJSON(st, WDqd.class);
                if (!teacher.getTotal().equals("0")) {

                    ccList = teacher.getRows();

                    bank_card_adapter.addItem(ccList);

                } else {
                    Toast.makeText(XianYiRen_activity.this, "暂无数据", Toast.LENGTH_SHORT).show();

                }
            }

            @Override
            public void onFalia(int code, String errst) {
                Toast.makeText(XianYiRen_activity.this, "网络错误！", Toast.LENGTH_SHORT).show();
            }
        });
    }


    @OnClick({R.id.img_back, R.id.button2})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.button2:
                startActivity(new Intent(this, Curriculum_Activity.class));
                break;
        }
    }
}
